﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using NewDDSys.Entity;
using NewDDSys.IBLL;
using NewDDSys.IDAL;
using NewDDSys.Factory;
using NewDDSys.Utility;
using AjaxControlToolkit;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Web_KFFWdt : System.Web.UI.Page
{
    private string num = "";
    private int good = 0;
    private int cs = 0;
    private DateTime pd;
    private DateTime zw;
    private int cc = 0;
    private int pb = 0;
    private int qh = 0;
    private string ku = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        num = Request.QueryString["Num"];
        DataSet ds = BLLFactory.CreateWorkBLL().Select(num);
        if (ds == null)
            Response.Write(" <script   language=javascript> window.alert( '无此记录，请联系管理员！ ');</script> ");
        else
        {
            txtNUM.Text = num;
            txtNUM.Attributes["contentEditable"] = "false";
            txtBanbie.Text = ds.Tables[0].Rows[0]["banbie"].ToString();
            txtBanbie.Attributes["contentEditable"] = "false";
            txtBanzu.Text = ds.Tables[0].Rows[0]["banzu"].ToString();
            txtBanzu.Attributes["contentEditable"] = "false";
            txtTianbao.Text = ds.Tables[0].Rows[0]["tianbao"].ToString();
            txtTianbao.Attributes["contentEditable"] = "false";
            txtKu.Text = ds.Tables[0].Rows[0]["ku"].ToString();
            ku = txtKu.Text.ToString();
            txtKu.Attributes["contentEditable"] = "false";
            txtDun.Text = ds.Tables[0].Rows[0]["dun"].ToString();
            txtDun.Attributes["contentEditable"] = "false";
            txtNeirong.Text = ds.Tables[0].Rows[0]["neirong"].ToString();
            txtNeirong.Attributes["contentEditable"] = "false";
            txtPos.Text = ds.Tables[0].Rows[0]["pos"].ToString();
            txtPos.Attributes["contentEditable"] = "false";
            txtPDtime.Text = ds.Tables[0].Rows[0]["pdtime"].ToString();
            pd = Convert.ToDateTime(txtPDtime.Text.ToString());
            txtPDtime.Attributes["contentEditable"] = "false";
            txtWcs.Text = ds.Tables[0].Rows[0]["wcs"].ToString();
            txtWcs.Attributes["contentEditable"] = "false";
            
            txtGood.Text = ds.Tables[0].Rows[0]["good"].ToString();
            good = Convert.ToInt32(txtGood.Text.ToString());
            cs = good;
            txtGood.Attributes["contentEditable"] = "false";
            txtBad.Text = ds.Tables[0].Rows[0]["bad"].ToString();
            txtBad.Attributes["contentEditable"] = "false";
            txtPb.Text = ds.Tables[0].Rows[0]["pb"].ToString();
            pb = Convert.ToInt32(txtPb.Text.ToString());
            txtPb.Attributes["contentEditable"] = "false";
            txtCc.Text = ds.Tables[0].Rows[0]["cc"].ToString();
            cc = Convert.ToInt32(txtCc.Text.ToString());
            txtCc.Attributes["contentEditable"] = "false";
            txtQH.Text = ds.Tables[0].Rows[0]["qh"].ToString();
            qh = Convert.ToInt32(txtQH.Text.ToString());
            txtQH.Attributes["contentEditable"] = "false";
            txtKx.Text = ds.Tables[0].Rows[0]["kx"].ToString();
            txtKx.Attributes["contentEditable"] = "false";
            txtZW.Text = ds.Tables[0].Rows[0]["zwtime"].ToString();
            
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        string fwtime = DateTime.Now.ToString();
        int f = BLLFactory.CreateWorkBLL().UpdateFw(num, fwtime);
        if (f >= 1)
        {
            Response.Write(" <script   language=javascript> window.alert( ' 已封完！ ');</script> ");
        }
        TimeSpan ts = Convert.ToDateTime(fwtime) - pd;
        decimal hs = Convert.ToDecimal(ts.TotalHours.ToString("f"));
        switch (ku)
        {
            case "不锈钢冷轧库":
            case "酸洗库":
                {
                    
                    

                    if (cs <= 6)
                    {
                        decimal yst = hs - Convert.ToDecimal(0.5 * cs + 4.5);
                        BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                    }
                    else if (cs <= 10)
                    {
                        decimal yst = hs - Convert.ToDecimal(5.5 + 0.5 * cs + 0.16 * (cs - 6));
                        BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                    }
                    else
                    {
                        decimal yst = hs - Convert.ToDecimal(1.5 + 4 + 0.33 * (cs - 10) + 0.5 * cs);
                        BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                    }
                }; break;
            case "不锈钢热轧库":
            case "碳钢热轧库":
            case "嘉东卷板":
                {
                    
                    if (cs <= 6)
                    {
                        decimal yst = hs - Convert.ToDecimal(0.5 * cs);
                        BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                    }
                    else
                    {
                        decimal yst = hs - 4;
                        BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                    }
                }; break;
            case "碳钢冷轧库":
                {
                    if (cs <= 6)
                    {
                        decimal yst = hs - Convert.ToDecimal(0.5 * cs + 1);
                        BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                    }
                    else if (cs <= 10)
                    {
                        decimal yst = hs - 5;
                        BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                    }
                    else
                    {
                        decimal yst = hs - Convert.ToDecimal(5 + 0.33 * (cs - 10));
                        BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                    }
                }; break;
            
            default:
                break;
        }
        
        if (txtYuanyin.Text.ToString() != "")
            BLLFactory.CreateWorkBLL().UpdateWorkYuanyin(num, txtYuanyin.Text.ToString());
        string sta = "封完";
        BLLFactory.CreateWorkBLL().UpdateSta(num, sta);
        Response.Redirect(string.Format("WORK.aspx?Sta={0}", sta));
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        zw = Convert.ToDateTime(txtZW.Text.ToString());
        int f = BLLFactory.CreateWorkBLL().UpdateZw(num, txtZW.Text.ToString());
        if (f >= 1)
        {
            Response.Write(" <script   language=javascript> window.alert( ' 装完时间已修改！ ');</script> ");
            DataSet ds = BLLFactory.CreateDxcBLL().SelectNum(num);
            if (ds != null)
            {
                string banzu = ds.Tables[0].Rows[0]["cbanzu"].ToString();
                BLLFactory.CreateDxcBLL().UpdateCL(num, "已处理", zw.ToString(), banzu);
            }
            TimeSpan ts = zw - pd;
            decimal zc = Convert.ToDecimal(ts.TotalHours.ToString("f"));
            BLLFactory.CreateWorkBLL().UpdateZcys(num, zc);
            if (txtNeirong.Text.ToString() == "零提")
            {
                if (Session["用户信息.部门"].ToString() == "嘉东库")
                {
                    decimal yst = zc - 5 * good;
                    BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                }
                else
                {
                    decimal yst = zc - 6 * good;
                    BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                }

            }
            else
            {
                switch (ku)
                {
                    case "板材库":
                        {
                            if (cs <= 6)
                            {
                                decimal yst = zc - Convert.ToDecimal(0.5 * cs);
                                BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                            }
                            else
                            {
                                decimal yst = zc - 4;
                                BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                            }
                        }; break;

                    case "一高线":
                        {
                            decimal yst = zc - Convert.ToDecimal(0.5 * cc + pb + 0.75 * qh);
                            BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                        }; break;
                    case "大棒线":
                        {
                            decimal yst = zc - Convert.ToDecimal(0.5 * cc + 0.83 * pb);
                            BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                        }; break;
                    case "嘉东线材":
                        {
                            decimal yst = zc - Convert.ToDecimal(0.5 * cs);
                            BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                        }; break;
                    case "嘉东螺纹":
                        {
                            decimal yst = zc - Convert.ToDecimal(0.75 * cc + pb);
                            BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                        }; break;

                    case "嘉东板材":
                        {
                            if (cs <= 6)
                            {
                                decimal yst = zc - Convert.ToDecimal(0.5 * cs);
                                BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                            }
                            else
                            {
                                decimal yst = zc - 4;
                                BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                            }
                        }; break;
                    case "二高线":
                        {
                            decimal yst = zc - Convert.ToDecimal(0.5 * cs);
                            BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                        }; break;
                    case "四轧库":
                    case "化产库":
                        {
                            decimal yst = zc - Convert.ToDecimal(0.66 * cs);
                            BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                        }; break;
                    default:
                        break;

                }
                    
            }
        }
        Response.Redirect(string.Format("WORK.aspx?Sta={0}", "装完"));
            
    }
}
